//
//  PrefectureViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PrefectureViewController.h"

#import "PreDetailViewController.h"

#import "PreDeTableViewCell.h"

#import "PreModel.h"

#define precell @"cell"
@interface PrefectureViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArry;


@end

@implementation PrefectureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    
    self.dataArry = [NSMutableArray array];
    self.tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    
    [self.tableview registerClass:[PreDeTableViewCell class] forCellReuseIdentifier:precell];
    [self getDataHandle];
}

- (void)getDataHandle
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.dongting.com/misc/musiczone/new?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            PreModel *model = [[PreModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArry addObject:model];
        }
//        NSLog(@"%@", self.dataArry);
        
        [self.tableview reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PreDeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:precell forIndexPath:indexPath];
    PreModel *premodel = self.dataArry[indexPath.row];
//    NSLog(@"%@", premodel);
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:premodel.picUrl]];
    cell.titleName.text = premodel.name;
    
    cell.label.text = premodel.desc;
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PreDetailViewController *preDetailVC = [[PreDetailViewController alloc] init];
    PreModel *model = self.dataArry[indexPath.row];
    
    preDetailVC.string = model.actionUrl;
    
    
    [self.navigationController pushViewController:preDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
